package org.sith.taskrunner.ejb.db.dao.impl;

import org.sith.taskrunner.api.toolkit.TaskToolkit;
import org.sith.taskrunner.api.user.Role;
import org.sith.taskrunner.ejb.annotations.AllowedRoles;
import org.sith.taskrunner.ejb.db.dao.AbstractDAO;
import org.sith.taskrunner.ejb.db.dao.ToolkitDAO;
import org.sith.taskrunner.ejb.db.entity.TaskToolkitEntity;

import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.Query;
import java.util.List;
import java.util.Set;

@Stateless(name = "ToolkitDAOService")
public class ToolkitDAOService extends AbstractDAO<TaskToolkitEntity> implements ToolkitDAO {


    public ToolkitDAOService() {

    }

    @Override
    protected Class<TaskToolkitEntity> getEnityClass() {
        return TaskToolkitEntity.class;
    }

    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    @Override
    public TaskToolkitEntity findByNameAndVersion(String toolkitBuilderName, Double version) {
        Query findByNameAndVersion = persistenceContext.createNamedQuery("findByNameAndVersion");
        findByNameAndVersion.setParameter("toolkitName", toolkitBuilderName).setParameter("version", version);
        return (TaskToolkitEntity) findByNameAndVersion.getSingleResult();
    }

    @Override
    public List<TaskToolkit> findActiveToolkits() {
        return  persistenceContext.createNamedQuery("findActiveToolkits").getResultList();
    }

    @Override
    public List<TaskToolkit> findAllToolkits() {
        return  persistenceContext.createNamedQuery("findAllToolkits").getResultList();
    }

    @Override
    public List<TaskToolkit> findSystemToolkits() {
        return persistenceContext.createNamedQuery("findSystemToolkits").getResultList();
    }

    @Override
    public boolean updateToolkitActiveState(Long id, boolean active) {

        Query updateToolkitActiveState = persistenceContext.createNamedQuery("updateToolkitActiveState");
        updateToolkitActiveState.setParameter("active", active);
        updateToolkitActiveState.setParameter("id", id);

        return updateToolkitActiveState.executeUpdate() == 0;
    }

    @Override
    public void updateAllToolkitActiveState(boolean active) {
        Query query = persistenceContext.createNamedQuery("updateAllToolkitActiveState");
        query.setParameter("active", active);
        query.executeUpdate();
    }

    @Override
    public TaskToolkitEntity getActiveToolkit(String name, Double version) {
        Query query = persistenceContext.createNamedQuery("findActiveToolkit").setParameter("name", name).setParameter("version", version);
        return (TaskToolkitEntity) query.getSingleResult();
    }

    @Override
    public boolean updateToolkitActiveState(Class clazz, boolean active) {
        Query query = persistenceContext.createNamedQuery("updateAllToolkitActiveStateByClassName");
        query.setParameter("active", active);
        query.setParameter("clazz", clazz.getCanonicalName());
        return query.executeUpdate()!=0;
    }


    @Override
    @AllowedRoles(Role.SYSTEM)
    public void removeAll() {
        throw new IllegalStateException("Not implemented");
    }
}
